<!--#
layout("/layouts/platform.html"){
#-->
<style>
    .el-upload input[type=file] {
        display: none;
    }

    .el-upload__text {
        color: #606266;
        font-size: 14px;
        text-align: center;
    }
</style>
<div id="app"  v-cloak>
    <!--<div class="head"><div class="headone"><i class="el-icon-menu"></i><span >客户信息</span></div></div>-->
    <el-row class="header navbar bg-white">
        <div class="btn-group tool-button" style="line-height: 50px;">
            公司名称:<el-input placeholder="公司名称" v-trim v-model="pageForm.name" size="mini" style="width:180px;"
                      clearable></el-input>
            <el-button slot="append" icon="el-icon-search" size="mini" @click="doSearch" circle></el-button>
        </div>
        <div class="pull-right offscreen-right" style="margin-right: 20px;line-height: 50px;">
             <el-tooltip class="item" effect="dark" content="新增">
                    <el-button type="primary" icon="el-icon-plus"  size="mini" @click="openAddDialog" circle></el-button>
             </el-tooltip>
        </div>
    </el-row>

    <el-row class="el-table-container">
        <el-table :data="tableData" @sort-change='pageOrder' height="520"  border size="small" header-align="center"  style="width: 100%" @selection-change="handleSelectionChange">
            <el-table-column sortable="custom" prop="name" label="公司名称" width="200px;" header-align="center" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column sortable="custom" prop="contacts" label="企业法人" width="120px;" header-align="center" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column sortable="custom" prop="phone" label="统一信用代码" width="180px;" header-align="center" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column sortable="custom" prop="telephone" label="电话" width="120px;" header-align="center" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column sortable="custom" prop="postalCode" label="邮编" width="120px;" header-align="center" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column sortable="custom" prop="address" label="地址" min-width="120px;" header-align="center" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column sortable="custom" prop="remark" label="备注" min-width="120px;" header-align="center" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column
                    prop="note"
                    label="操作"
                    width="120">
                <template slot-scope="scope">
                    <el-dropdown @command="dropdownCommand">
                        <el-button size="mini">
                            <i class="ti-settings"></i>
                            <span class="ti-angle-down"></span>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item divided :command="{type:'edit',row:scope.row}">
                                修改
                            </el-dropdown-item>
                            <el-dropdown-item divided :command="{type:'delete',row:scope.row}">
                                删除
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>
    </el-row>
    <el-row class="el-pagination-container">
        <el-pagination @size-change="pageSizeChange" @current-change="pageNumberChange" :current-page="pageForm.pageNumber" :page-sizes="[10, 20, 30, 50]"
                       :page-size="pageForm.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageForm.totalCount">
        </el-pagination>
    </el-row>

    <el-dialog :close-on-click-modal="false" append-to-body   title="客户信息"  :visible.sync="addDialogVisible" width="50%">
        <el-form id="addForm"  :rules="formRules" :model="formData" ref="addForm"  size="small" label-width="120px"  style="width: 80%">
            <el-form-item prop="name" label="公司名称" >
                <el-input maxlength="25"v-trim v-model="formData.name" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="address" label="地址" >
                <el-input maxlength="75"v-trim v-model="formData.address" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="contacts" label="企业法人">
                <el-input maxlength="16"v-trim v-model="formData.contacts" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="phone" label="统一信用代码">
                <el-input maxlength="25"v-trim v-model="formData.phone" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="telephone" label="电话">
                <el-input maxlength="16"v-trim v-model="formData.telephone" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="postalCode" label="邮编">
                <el-input maxlength="16"v-trim v-model="formData.postalCode" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="remark" label="备注">
                <el-input maxlength="125" v-trim v-model="formData.remark" type="textarea"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="doAdd">确 定</el-button>
        </span>
    </el-dialog>
</div>
<style scoped>
    .el-table--border th.gutter:last-of-type {
        display: block !important;
        width: 17px !important;
    }

</style>
<script>
    new Vue({
        el: "#app",
        data: function () {
            return {
                tableData: [],
                addDialogVisible:false,
                pageForm: {},
                formData: {},
                formRules: {
                    name: [{required: true, message: '公司名称不可为空', trigger: ['blur', 'change']}],
                    phone:[ {required: true, message: '信用代码不可为空！', trigger: ['blur', 'change']},
                            {pattern:  /^([0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}\(\w{1}\-\w{1}\)|[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}|[1-9]\d{14})$|^\/$/, message: '输入15或18位号码、英文括号“例(1-1)”或斜杠/'}],
                    telephone:[ {required: true, message: '电话号码不可为空！', trigger: ['blur', 'change']},
                        {pattern: /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$|^\((0[0-9]{2,3})\)-?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$|^1[3456789]\d{9}$|^\/$/, message: '只能输入手机号码、座机号码或斜杠/'}],
                    postalCode:[ {required: true, message: '邮编不可为空！', trigger: ['blur', 'change']},
                        {pattern: /^\d{6}$|^\/$/, message: '邮编只能是6位数字或斜杠/'}],
                    address: [{required: true, message: '地址不可为空', trigger: ['blur', 'change']}],
                    contacts: [{required: true, message: '企业法人不可为空', trigger: ['blur', 'change']}],
                },
            }
        },
        methods: {
            doAdd: function () {
                var self = this;
                self.$refs["addForm"].validate(function (valid) {
                    if (valid) {
                        var url = "/platform/bus/company/addDo";
                        if(nONull(self.formData.id)){
                            url = "/platform/bus/company/editDo";
                        }
                        $.post(base + url, self.formData, function (data) {
                            if (data.code == 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.addDialogVisible = false;
                                self.pageData();//添加失败也可以入库,所以加载一下看看
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
            openAddDialog:function(){
                this.addDialogVisible=true;
                this.formData = {
                    id: "",
                    name:"",
                    address:"",
                    contacts: "",
                    phone:"",
                    telephone:"",
                    postalCode:"",
                    remark: ""
                };//打开新增窗口,表单先清空
                if (this.$refs["addForm"])
                    this.$refs["addForm"].resetFields();
            },
            pageOrder: function (column) {//按字段排序
                this.pageForm.pageOrderName = column.prop;
                this.pageForm.pageOrderBy = column.order;
                this.pageData();
            },
            pageNumberChange: function (val) {//页码更新操作
                this.pageForm.pageNumber = val;
                this.pageData();
            },
            pageSizeChange: function (val) {//分页大小更新操作
                this.pageForm.pageSize = val;
                this.pageData();
            },
            pageData: function () {//加载分页数据
                var self = this;
                sublime.showLoadingbar();//显示loading
                $.post(base + "/platform/bus/company/data", self.pageForm, function (data) {
                    sublime.closeLoadingbar();//关闭loading
                    if (data.code == 0) {
                        self.tableData = data.data.list;
                        self.pageForm.totalCount = data.data.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            doSearch: function () {
                this.pageForm.pageNumber = 1;
                this.pageData();
            },

            dropdownCommand: function (command) {//监听下拉框事件
                var self = this;
                if ("delete" == command.type) {
                    self.$confirm('此操作将删除 ' + command.row.name, '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        callback: function (a, b) {
                            if ("confirm" == a) {//确认后再执行
                                $.post(base + "/platform/bus/company/delete/" + command.row.id, {}, function (data) {
                                    if (data.code == 0) {
                                        self.$message({
                                            message: data.msg,
                                            type: 'success'
                                        });
                                        self.doSearch();
                                    } else {
                                        self.$message({
                                            message: data.msg,
                                            type: 'error'
                                        });
                                    }
                                }, "json");
                            }
                        }
                    });
                }
                if("edit" == command.type){//修改
                    //根据id获取数据
                    $.post(base + "/platform/bus/company/detail/" + command.row.id, {}, function (data) {
                        if (data.code == 0) {
                            self.formData =  data.data;
                        } else {
                            self.$message({message: data.msg,type: 'error'});
                        }
                    }, "json");
                    //打开窗口
                    this.addDialogVisible=true;
                }
            },
            getDict: function (val,map) {
                if (val!= null && val!=undefined &&val!="" &&val!="null" &&val!="undefined" && map!=null && map !=undefined){
                    var str = val.split(",");
                    var strname="";
                    for (var a=0;a<str.length;a++){
                        strname+=map[str[a]];
                        if(strname == null){
                            alert(1)
                        }
                        if(a<(str.length-1)){
                            strname+="/";
                        }
                    }
                    return strname;
                }
                return "";
            },
            handleSelectionChange: function (val) {
                this.multipleSelection = val;
            }
        },
        created: function () {
            this.doSearch();
        }
    });

    function nONull(val){
        if (val!= null && val!=undefined &&val!="" &&val!="null" &&val!="undefined" ){
            return true;
        }else{
            return false;
        }
    }
</script>
<!--#
}
#-->
